<template>
  <div class="test-container">
    <search-header
      v-show="boo.showSearch"
      :select-prop="selectProp"
      @search-action="searchAction"
    ></search-header>
    <el-button @click="showDialog('plot', 'add')">测试</el-button>
    <dialog-normal
      ref="dialog-plot"
      :dialog-prop="{
        validete: true,
        title: '测试',
        width: '1000px',
        height: '30px',
        submit: '确定',        
        close: '取消'
      }"
      @normal-submit="submitDialog"
    >
      <template #dialog-content>
        123123123<br/>
        123123123<br/>
        123123123<br/>
        123123123<br/>
        123123123<br/>
        123123123<br/>
      </template>
    </dialog-normal>
  </div>
</template>

<script>

export default {
  name: 'Page404',
  components: {
    "search-header": () => import("@/rhComponents/searchHeader/index"),
    "dialog-normal": () => import("@/rhComponents/dialog/normal-dialog"),
  },
  computed: {
    message() {
      return '找不到网页！'
    }
  },
  data(){
    return {
      selectProp: JSON.parse(JSON.stringify(this.$getSearchHeader(this))),
      boo: {
        loading: false, // 遮罩层
        multiple: true, // 非多个禁用
        showSearch: true, // 显示搜索条件
      },
      queryParams: {}
    }
  },
  methods: {
    // 搜索条件查询
    searchAction(prop) {
      this.queryParams = Object.assign(this.queryParams, prop["params"]);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 事件集合 -- 转发
    actionsMaps(ref, acion, row){
      let _self = this
      let actionsMap = () => {
        return new Map([
          [{ref: 'device', acion: 'edit:show'}, ()=>{
            _self.$set(row, 'editDevice', true)
            _self.dialogDevice.currentItem = row
            this.getDeviceList().then(
              res => {
                _self.$set(row, 'editDevice', false)
                _self.$nextTick(() => {
                  _self.$refs[`dialog-${ref}`].showDialog = true
                })
              }
            ).catch(err => _self.$set(row, 'editDevice', false))
          }],
          [{ref: 'device-detail', acion: 'view:show'}, ()=>{
            _self.$set(row, 'viewDevice', true)
            getDeviceList({landId: row.landId}).then(
              res => {
                _self.dialogDevice.boundDeviceList = []
                _self.$set(row, 'viewDevice', false)
                _self.dialogDevice.boundDeviceList = res.data.boundDeviceList
                _self.$nextTick(() => {
                  _self.$refs[`dialog-${ref}`].showDialog = true
                })
              }
            ).catch(err => _self.$set(row, 'viewDevice', false))
          }],
          [{ref: 'device', acion: 'edit:submit'}, ()=>{
            // if(_self.dialogDevice.boundDeviceList.length == 0) {
            //   _self.$refs[`dialog-${ref}`].loading = false
            //   return _self.$message.error('请选择需要绑定设备')
            // } 
            _self.bindDevice().then(
              res => {
                _self.$refs[`dialog-${ref}`].loading = false
                _self.$refs[`dialog-${ref}`].showDialog = false
                _self.getList()
              }
            ).catch(err => _self.$refs[`dialog-${ref}`].loading = false)
          }],
          [{ref: 'plot', acion: 'add:show'}, ()=>{            
            _self.$nextTick(() => {
              _self.$refs[`dialog-${ref}`].showDialog = true
            })
          }],
          [{ref: 'plot', acion: 'edit:show'}, ()=>{
            _self.dialogPloat.title = '编辑地块'
            _self.dialogPloat.list.forEach(item => {
              let idx = _self.selectProp.selectList.findIndex(i => i.deptType == item.deptType)
              idx != -1 && _self.$set(item, 'child', _self.selectProp.selectList[idx]['child'])
            })
            _self.$set(row, 'editLand', true)
            getLandDetail(row.landId).then(
              res => {
                _self.$set(row, 'editLand', false)
                res.data.landType = parseInt(res.data.landType)
                res.data.companyId = parseInt(res.data.companyId)
                _self.dialogPloat.currentItem = res.data
                _self.dialogPloat.currentItem = res.data
                _self.$set(_self.dialogPloat.currentItem, 'landRegion', res.data.areaVO.id)
                _self.selectChange(_self.dialogPloat.currentItem['companyId'], {name: 'companyId'}, 'init')
                _self.$nextTick(() => {
                  _self.$refs[`dialog-${ref}`].showDialog = true
                  setTimeout(() => {
                    _self.$refs['draw-map'].searchByAreaName({acode: res.data['landRegion'], notReview: true})
                  })
                })
              }
            ).catch(err => _self.$set(row, 'editLand', true))
          }],
          [{ref: 'plot', acion: 'add:submit'}, ()=>{
            _self.dialogPloat.currentItem['picUrls'] && delete _self.dialogPloat.currentItem['picUrls']
            let params = JSON.parse(JSON.stringify(_self.dialogPloat.currentItem))
            params.fileIds = params.fileIds || []
            let arrFile = [], arrPath = []
            params.fileIds.forEach(file => {
              arrFile.push(file.id || file.fileId)
            })
            params.fileIds = arrFile.toString()
            if(!params.landLatList || params.landLatList.length == 0) {
              _self.$refs[`dialog-${ref}`].loading = false
              return _self.$message.error('请先进行地图划区')
            }
            params.landLatList.forEach(point => {
              if(point['lng'] || point['lat']){
                arrPath.push([point['lng'], point['lat']])
              }
            })
            params.landLatList = arrPath.length != 0 ? arrPath : params.landLatList
            let reg = /^(0|[1-9]\d*)(\.\d{1,2})?$/
            
            if(!reg.test(params.landArea)) {
              _self.$refs[`dialog-${ref}`].loading = false
              return _self.$message.error('地块面积为保留两位小数的数值')
            }
            addLand(params).then(
              res => {
                _self.$message.success('操作成功')
                _self.$refs[`dialog-${ref}`].loading = false
                _self.$refs[`dialog-${ref}`].showDialog = false
                _self.getList()
              }
            ).catch(err => _self.$refs[`dialog-${ref}`].loading = false)
          }]
        ])
      }
      let actions = [...actionsMap()].filter(([key,value])=>(key.ref == ref && key.acion == acion))
      actions.forEach(([key,value])=>value.call(this))
    },
    showDialog(ref, type, row) {
      let _self = this;
      if (_self.$refs[`${ref}-form`]) {
        this.resetForm(`${ref}-form`);
      }
      _self.$nextTick(() => {
        _self.actionsMaps(ref, `${type}:show`, row)
      })
    },
    submitDialog(){
      
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
